package io.gitee.twoke.generator.engine.impl.generate.impl;

import io.gitee.twoke.generator.engine.GenerateService;
import io.gitee.twoke.generator.engine.impl.ServiceImpl;
import io.gitee.twoke.generator.engine.impl.cfg.GenerateEngineConfigurationImpl;
import io.gitee.twoke.generator.engine.impl.cmd.*;
import io.gitee.twoke.generator.engine.impl.generate.*;


public class GenerateServiceImpl  extends ServiceImpl
        implements GenerateService {
    public GenerateServiceImpl(GenerateEngineConfigurationImpl processEngineConfiguration) {
        super(processEngineConfiguration);
    }

    public GenerateServiceImpl() {
    }

    @Override
    public DataTableQuery createDataTableQuery() {
        return new DataTableQueryImpl(commandExecutor, generateEngineConfiguration.getDatabaseType());
    }

    @Override
    public DataTableColumnQuery createDataTableColumnQuery() {
        return new DataTableColumnQueryImpl(commandExecutor, generateEngineConfiguration.getDatabaseType());
    }

    @Override
    public ImportTableBuilder createImportTable() {
        return new ImportTableBuilderImpl(this);
    }

    @Override
    public void deleteTableByName(String tableName) {
        commandExecutor.execute(new DeleteTableCmd(tableName, DeleteTableCmd.DeleteType.BY_TABLE_NAME));
    }

    @Override
    public void deleteTableById(String tableId) {
        commandExecutor.execute(new DeleteTableCmd(tableId,  DeleteTableCmd.DeleteType.BY_TABLE_ID));
    }

    @Override
    public void updateTable(DataTable table) {
        commandExecutor.execute(new UpdateTableCmd(table));
    }

    @Override
    public void updateColumn(DataTableColumn column) {
        commandExecutor.execute(new UpdateColumnCmd(column));
    }

    @Override
    public void generate(String id) {
        commandExecutor.execute(new GenerateCodeOfTableConfigCmd(id, generateEngineConfiguration));
    }

    public void importTable(ImportTableBuilderImpl importTableBuilder) {
        if (importTableBuilder.isLoadDatabase()) {
            commandExecutor.execute(new ImportTableCmd(importTableBuilder.getTable().getName()));
        } else {
            commandExecutor.execute(new ImportTableCmd(importTableBuilder.getTable(), importTableBuilder.getColumns()));
        }
    }
}
